package handlers

import (
	"GoServer/common"
	"GoServer/dataaccess"
	"GoServer/entrance/game/game_service"
	"GoServer/message"
	"GoServer/models"
	"GoServer/utils/debug"
	"GoServer/utils/tool"
	"fmt"
	"github.com/gin-gonic/gin"
)

// / 查询主播信息
// / 查询主播分数
// / 查询主播解锁的角色和场景
func OnAnchorScore(c *gin.Context) {
	anchorId := c.Query("anchor_id")
	if anchorId == "" {
		tool.Resp(c.Writer, message.ErrorMsg, nil, "")
		return
	}

	info, err := dataaccess.GetAnchorInfo(anchorId)
	if err != nil {
		tool.Resp(c.Writer, message.ErrorMsg, nil, "获取主播信息失败")
		return
	}

	tool.Resp(c.Writer, message.Success, gin.H{
		"anchor_id":      info.AnchorId,
		"score":          info.Score,
		"unlocked_items": info.UnlockItemId,
	}, "")
}

// / 主播兑换礼物
func OnAnchorExchange(c *gin.Context) {
	itemId := c.Query("item_id")
	anchorId := c.Query("anchor_id")
	if itemId == "" || anchorId == "" {
		tool.Resp(c.Writer, message.ErrorMsg, gin.H{"error": "缺少必要参数"}, "")
		return
	}

	if err := dataaccess.UnlockItemForAnchor(anchorId, itemId); err != nil {
		tool.Resp(c.Writer, message.ErrorMsg, gin.H{"error": "道具兑换失败"}, "")
		return
	}

	tool.Resp(c.Writer, message.Success, gin.H{"anchor_id": anchorId, "item_id": itemId}, "道具兑换成功")
}

// / 游戏结束
func OnGameEnd(c *gin.Context) {
	roomId := c.Query("room_id")

	if roomId == "" {
		tool.Resp(c.Writer, message.ErrorMsg, nil, "游戏名称错误")
		return
	}

	if !common.HasScoreStrategy(common.GameName) {
		tool.Resp(c.Writer, message.ErrorMsg, nil, "游戏未注册")
		return
	}

	fianlScore := game_service.SettleAnchorScore(common.GameName, roomId)
	debug.Logf(debug.Info, fmt.Sprintf("当局游戏结束，积分池：[%d]， 主播得分: [%d]", fianlScore, 0))

	anchor := models.AnchorInfoBasic{
		AnchorId:     roomId,
		Score:        fianlScore,
		UnlockItemId: []uint{},
	}

	tool.Resp(c.Writer, message.Success, anchor, "游戏结算完成")
}
